# -*- coding: utf-8 -*-
'''
Created on 12.12.2010
@author: imagovrn
'''

from pygraph.classes.graph import graph
from solution import Solution
import unittest
import random

DOT_EXE = "c:\\Program Files (x86)\\Graphviz2.26.3\\bin\\dot.exe"

def prepare_graph():
    result = graph()
    result.DIRECTED = False
    
    result.add_nodes([1, 2, 3, 4, 5, 6])
    
    result.add_edge((1, 2), 1)
    result.add_edge((1, 3), 3)
    result.add_edge((2, 4), 1)
    result.add_edge((2, 6), 3)
    result.add_edge((3, 4), 1)
    result.add_edge((3, 5), 1)
    result.add_edge((4, 5), 3)
    result.add_edge((5, 6), 1)
    
    return result
    
def solution_population():
    return Solution.from_nodelists(prepare_graph(),
                                   [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [3, 6]]) 

def main():
    gr = prepare_graph()
    solutions = solution_population()
    assert len(solutions) % 2 == 0
    
    solutions = sorted(solutions, key=lambda solution:solution.target_value())
    print [sol.target_value() for sol in solutions]
    print solutions
    
    for i in xrange(10):
        first_family = solutions[0::2]
        second_family = solutions[1::2]
        
        solutions = list()
        for i in xrange(len(first_family)):
            solutions.extend(first_family[i].crossover(second_family[i]))
        solutions = sorted(solutions, key=lambda solution:solution.target_value())
        print solutions
    
class PartitioningTest(unittest.TestCase):
    """
    Test case for partitioning
    """
    def __init__(self, methodName):
        """
        Constructor: initializes members
        """
        unittest.TestCase.__init__(self, methodName)
        
        self._graph = None
    
    def setUp(self):
        """
        Set up the environment
        """
        self._graph = graph()
        self._graph.DIRECTED = False
        
        self._graph.add_nodes([1, 2, 3, 4, 5, 6])
        
        self._graph.add_edge((1, 2), 1)
        self._graph.add_edge((1, 3), 3)
        self._graph.add_edge((2, 4), 1)
        self._graph.add_edge((2, 6), 3)
        self._graph.add_edge((3, 4), 1)
        self._graph.add_edge((3, 5), 1)
        self._graph.add_edge((4, 5), 3)
        self._graph.add_edge((5, 6), 1)
        
    def test_estimates(self):
        node_sets = [set(list) for list in [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [3, 6]]]
        estimates = calculate_estimates(self._graph, node_sets)
        
        self.assertEquals(len(estimates), 6)
        
        self.assertEquals(estimate_as_string(estimates[0]), "1 >= 7")
        self.assertEquals(estimate_as_string(estimates[1]), "0 >= 10")
        self.assertEquals(estimate_as_string(estimates[2]), "1 >= 8")
        self.assertEquals(estimate_as_string(estimates[3]), "3 >= 4")
        self.assertEquals(estimate_as_string(estimates[4]), "1 >= 7")
        self.assertEquals(estimate_as_string(estimates[5]), "0 >= 9")
        
    
if __name__ == "__main__":
    random.seed()
    main()
